package com.luckylo.activemq.local;

import com.luckylo.activemq.bean.Message;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 本地直连 队列模式
 *
 * @user: luckylo
 * @date: 2018-1-18
 */
public class QueueModel {

    private static Session session;


    static {
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void init() throws JMSException {
        // 获取工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();

        // 通过工厂创建连接
        Connection connection = factory.createConnection();

        // 只有开始之后才能获取 session
        connection.start();

        // 根据连接创建session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    public static void main(String[] args) throws JMSException {
        //point to point 队列模式
        Queue queue = session.createQueue("queue");

        MessageProducer producer = session.createProducer(queue);

        producer.send(session.createObjectMessage(new Message("mysql", "local", "test")));

    }


}
